/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.FileManager;
import control.GuiManager;
import control.boo.AbstractJavaTask;
import control.boo.LejosCompiler;
import control.boo.LejosUploader;
import java.awt.BorderLayout;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;
import org.fife.ui.rtextarea.RTextScrollPane;

/**
 *
 * @author Jojo
 */
public class CustomTabPanel extends JPanel{
    CustomEditorPane textEditor;
    RTextScrollPane scroller;
    FileManager fileMan;
    AbstractJavaTask boo;
    java.util.ResourceBundle langBundle = GuiManager.getLangBundle();
    
    public CustomTabPanel(File pFile){
        super();
        this.setLayout(new BorderLayout());
        textEditor = new CustomEditorPane();
        scroller = new RTextScrollPane(this.textEditor);
        this.textEditor.setScroller(scroller);
        this.add(scroller, BorderLayout.CENTER);
        this.configTextEditor();
        this.updateGuiEditor();
        this.fileMan=new FileManager(this.textEditor, pFile);        
        GuiManager.addEditorToList(textEditor); 
        textEditor.requestFocusInWindow(); 
    }
    
    private void configTextEditor(){
        //Scrollpane + Zeilenummern        
        this.textEditor.setAutoscrolls(true);
        scroller.setIconRowHeaderEnabled(false); //keine Auswirkungen
        scroller.setAutoscrolls(true);
        scroller.getGutter().setBookmarkingEnabled(false);
        this.textEditor.setStandardStyle();
        this.textEditor.insert("import egLejos.*; "+ "\n"+"\n"+"class NameDerKlasse {"
                + "\n"+ "\n"+ "\n"+ "\t"                
                + "void start(){\n"
                + "\t\t" + "DeineAnweisungen;\n"
                + "\t}"+ "\n"+ "\n"+"\t"
                + "public static void main(String[] args){\n"
                + "\t\t" + "new NameDerKlasse().start();\n"
                + "\t}" + "\n"+ "\n"+ "}", 0);
        this.textEditor.setCaretPosition(24);
        
        //this.textEditorPane.requestFocus();
        //this.textEditorPane.requestFocusInWindow();
    }
    
    public boolean isTextDirty(){
        return (this.textEditor.isDirty());
    }
    
    public boolean isDocumentEmpty(){
        return !(this.textEditor.getDocument().getLength()>0);
    }
    
    public void loadFileInEditor(File pFile) throws IOException{
        this.textEditor.loadAndShowFile(pFile);
    }
    
    
    private void updateGuiEditor(){
        this.textEditor.updateOptions();
    }


    void handleSaveFileAction() {
        this.fileMan.handleSaveFileAction();
    }

    void handleCloseFileAction() {
        this.fileMan.handleCloseFileAction();
    }

    void handleOpenFileAction() {
        this.fileMan.handleOpenFileAction();
        //System.err.println(this.textEditor.getEncoding());
    }
    
    void handleSaveFileAs(){
        this.fileMan.saveFileAs();
    }
    
    void handleNewFileAction(){
        this.fileMan.handleNewFileAction();
    }

    void interpreteWithBoo() {
        if(this.isSavedFile()){            
            if(this.isTextDirty()){
                if(GuiManager.getInstance().showYesNoMessage(langBundle.getString("MsgInfoSaveFileBoo"), langBundle.getString("MsgInfoTitleSave"))) {
                    this.handleSaveFileAction();                    
                }                
            }
            else{
                if(this.isDocumentEmpty()){
                    GuiManager.getInstance().showMessage(langBundle.getString("MsgInfoEmptyDocument"));
                }
                else{
                   boo = new LejosCompiler(this.fileMan.getActiveFile().getAbsolutePath());
                    boo.start();                    
                }
                
            }            
        }
        else{
            if(GuiManager.getInstance().showYesNoMessage(langBundle.getString("MsgInfoSaveFileReminder"), langBundle.getString("MsgInfoTitleSave"))) {
                this.handleSaveFileAction();         
            }
        }
        
        
    }

    void stopBooTask() {
        boo.stopLejosTask();
        boo = null;
    }

    void handleCut() {
        this.textEditor.cut();
    }

    void handleCopy() {
        this.textEditor.copy();
    }

    void handlePaste() {
        this.textEditor.paste();
    }

    boolean search(String pText, boolean b) {
        return this.textEditor.search(pText, b);
    }

    void handlePrint() throws PrinterException {
        this.textEditor.print();
    }

    void handleUndoLastAction() {
        if(this.textEditor.canUndo()){
            this.textEditor.undoLastAction();
        }        
    }

    void handleRedoLastAction() {
        if(this.textEditor.canRedo()){
            this.textEditor.redoLastAction();
        }
    }
    
    void handleSelectAll(){
        this.textEditor.selectAll();
    }
    
    boolean isSavedFile(){
        return this.textEditor.isLocalAndExists();
    }
    
    String getFilePath(){
        return this.textEditor.getFileFullPath();
    }

    void uploadToNxt() {
        if(this.isSavedFile()){            
            if(this.isTextDirty()){
                if(GuiManager.getInstance().showYesNoMessage(langBundle.getString("MsgInfoSaveFileBoo"), langBundle.getString("MsgInfoTitleSave"))) {
                    this.handleSaveFileAction();                    
                }                
            }
            else{
                if(this.isDocumentEmpty()){
                    GuiManager.getInstance().showMessage(langBundle.getString("MsgInfoEmptyDocument"));
                }
                else{
                    //boo = new LejosUploader(this.textEditor.getFileFullPath(), GuiManager.getInstance());
                    boo = new LejosUploader(this.fileMan.getActiveFile().getAbsolutePath());
                    boo.start();
                                     
                }
                
            }            
        }
        else{
            if(GuiManager.getInstance().showYesNoMessage(langBundle.getString("MsgInfoSaveFileReminder"), langBundle.getString("MsgInfoTitleSave"))) {
                this.handleSaveFileAction();         
            }
        }
        
        
        
        
        
        
        
        
        
        
         
    }

    public void setFocusToEditor() {
        this.textEditor.requestFocus(true);
    }
    
    
    
    
    
}
